<template>
  <div>
    <!-- 搜索分类部分 -->
    <div style="margin: 10px 0">
      <el-input style="width: 200px;margin-right: 5px;margin-bottom: 5px" suffix-icon="el-icon-search" placeholder="请输入商品名称" v-model="goodsName"></el-input >
      <el-select v-model="goodsTypeValue" clearable placeholder="请选择商品类型" style="width: 200px;margin-right: 5px;margin-bottom: 5px">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" style="background-color: #42b983" @click="handleAdd">新增<i class="el-icon-circle-plus-outline" style="margin-left: 5px"></i></el-button>
    </div>
     <!--新建、编辑表单-->
     <el-dialog title="商品信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品详情">
          <el-input v-model="form.goodsDetail" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品进价">
          <el-input v-model="form.goodsBid" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品售价">
          <el-input v-model="form.goodsPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品已售">
          <el-input v-model="form.goodsSold" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品库存">
          <el-input v-model="form.goodsStock" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
          class="avatar-uploader"
          :action="'http://localhost:9090/file/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
            <img v-if="form.goodsImg" :src="form.goodsImg" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>
        <!-- <el-form-item label="商品图片">
          <el-input v-model="form.goodsImg" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="商品类型">
          <el-select v-model="form.goodsType" clearable placeholder="请选择商品类型" style="width: 200px;margin-right: 5px;margin-bottom: 5px">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    
    <!-- 表格 -->
    <el-table :data="goodsData" border stripe >
      <el-table-column type="selection" width="55"  ></el-table-column>
      <el-table-column prop="goodsId" label="商品id" width="80"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称" width="140"></el-table-column>
      <el-table-column prop="goodsDetail" label="商品详情" width="140"></el-table-column>
      <el-table-column prop="goodsBid" label="商品进价" width="80"></el-table-column>
      <el-table-column prop="goodsPrice" label="商品售价" width="80"></el-table-column>
      <el-table-column prop="goodsSold" label="商品已售" width="80"></el-table-column>
      <el-table-column prop="goodsStock" label="商品库存" width="80"></el-table-column>
      <el-table-column  label="商品状态" width="140" prop="goodsState">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.goodsState" active-color="#13ce66" inactive-color="#ccc"
                     @change="changeEnable(scope.row)">
          </el-switch>
          <span v-text="scope.row.goodsState?'上架中':'已下架'" style="margin-left:2px"></span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsType" label="商品类型"></el-table-column>

      <!--编辑、删除操作-->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="warning" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              style="margin-left: 5px"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDelete(scope.row.goodsId)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-delete"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页区-->
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name:'goodsList',
  data(){
    return{
      goodsName:'',
      goodsTypeValue:'',
      goodsData:[],
      total:0,
      pageNum:1,
      pageSize:10,
      options: [{
          value: '居家',
          label: '居家'
        }, 
        {
          value: '食品',
          label: '食品'
        },{
          value: '服饰',
          label: '服饰'
        }, {
          value: '数码',
          label: '数码'
        }, {
          value: '文化',
          label: '文化'
        }, {
          value: '家电',
          label: '家电'
      }],
      //新增
      dialogFormVisible:false,
      form:{}
        
    }
  },
  methods:{
    //  请求页面查询数据
    async load(){
      let res=await this.request.get("/goods/page",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          goodsName:this.goodsName,
          goodsType:this.goodsTypeValue
        }
      })
      console.log('res:',res.data);
      this.goodsData=res.data.records
      this.total=res.data.total
      // console.log(res)
    },
    //重置搜索框
    reset(){
      this.goodsname=''
      this.goodsTypeValue=''
      this.load()
    },

    //点击新增按钮触发事件
    handleAdd(){
      console.log('点击了新增按钮');
      this.dialogFormVisible=true
      this.form={}
    },
    handleAvatarSuccess(res) {
      this.form.goodsImg = res
    },
    //点击表单提交按钮(新增用户or修改信息)确定事件
    async save(){
      this.form['goodsState']=false
      console.log(this.form);
      let res=await this.request.post("/goods",this.form)
      if(res.data){
        this.$message.success("操作成功！")
        this.dialogFormVisible=false
        this.load()
      }else{
        this.$message.error("操作失败！")
      }
    },
    // 商品状态切换触发函数
    async changeEnable(row) {
      this.form={}
      this.form=row
      // console.log('form:',this.form);
      let res=await this.request.post("/goods",this.form)
      if(res.code==='200'){
        this.$message.success('更新商品状态成功！')
      }else{
        this.$message.error('更新商品状态失败!')
      }
    },
    
    //#region (编辑、删除)
    //点击编辑按钮触发事件
    handleEdit(row){
      if(row.goodsState){
        this.$message.warning("商品未下架，不允许编辑！！！")
      }else{
        this.dialogFormVisible=true
        this.form=row
      }
    },
    //点击删除按钮触发事件
    async handleDelete(goodsId){
      let res=await this.request.delete("/goods/"+goodsId)
      if(res.data){
        this.$message.success("删除成功！")
        this.load()
      }else{
        this.$message.error("删除失败！")
      }
    },
    //#endregion

    //#region (分页)
    //分页(一页能容纳的最大数据多少条)
    handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.load()
    },
    //分页(点击跳到多少页)
    handleCurrentChange(pageNum){
      this.pageNum=pageNum
      this.load()
    },
    //#endregion
  },
  mounted(){
    this.load()
  }
}
</script>

<style scoped>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>